//
//  UICheckBox.m
//  radioButtonsDemo
//
//  Created by admin on 9/6/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "UICheckBox.h"

@implementation UICheckBox
@synthesize  checkBoxIndex;

-(id)initWithPosition:(CGPoint)position
{
    CGRect btnFrame = CGRectMake(position.x, position.y,
                                 CHECK_BOX_W,CHECK_BOX_H);
    self=[super initWithFrame:btnFrame];
    
    if (self) {
        [self setBackgroundImage:[UIImage imageNamed:CBNonSelectedImg] forState:UIControlStateNormal];
    }
    return  self;
    
}

-(void)setText:(NSString*)text
{
    UIFont *font= [UIFont fontWithName:@"arial" size:FONT_SIZE];
    CGSize labelSize = [text sizeWithFont:font constrainedToSize:CGSizeMake(MAX_WIDTH, MAX_HEIGHT) lineBreakMode:UILineBreakModeWordWrap];
    CGFloat labelHeight = labelSize.height;
    UILabel * textLabel = [[[UILabel alloc] init] autorelease];
    textLabel.text =text;
    [textLabel setFont:font];
    [textLabel sizeToFit];
    [textLabel setFrame:CGRectMake(CHECK_BOX_W+10,(CHECK_BOX_H-FONT_SIZE)/2 , 500,labelHeight)];
    [self addSubview:textLabel];
    [textLabel setNumberOfLines:0];
    [self setClipsToBounds:NO];
}

@end


@implementation UICheckBoxGroup
@synthesize selectedcheckBoxIndexes;
@synthesize Gid;

-(id)initWithTag:(int)tag;
{
    self = [super init];
    if(self)
    {
        selectedcheckBoxIndexes=[[NSMutableArray alloc] init];
        Gid = tag;
        cBoxCount = 1;
    }
    return self;
}

-(id)init
{
    self = [super init];
    if(self)
    {
        cBoxCount = 1;
        
    }
    return self;
}   

-(void)addButton:(UICheckBox *)checkBox
{
    [checkBox addTarget:self action:@selector(btnPressedAction:) forControlEvents:UIControlEventTouchUpInside];
    checkBox.checkBoxIndex = cBoxCount++;
}

-(void)btnPressedAction:(UICheckBox*)sender
{
    if(sender.isSelected)
    {
        [sender setSelected:NO];
        [sender setBackgroundImage:[UIImage imageNamed:CBNonSelectedImg] forState:UIControlStateNormal];
        [selectedcheckBoxIndexes removeObject:[NSNumber numberWithInt:sender.checkBoxIndex]];
    }
    else
    {
        [sender setSelected:YES];
        [sender setBackgroundImage:[UIImage imageNamed:CBselectedImg] forState:UIControlStateNormal];
        [selectedcheckBoxIndexes addObject:[NSNumber numberWithInt:sender.checkBoxIndex]];
        
    }

}

@end

